package com.limbo.showapp.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.limbo.showapp.R;
import com.limbo.showapp.activity.CartoonActivity;
import com.limbo.showapp.activity.PictureActivity;
import com.limbo.showapp.activity.VideoActivity;
import com.limbo.showapp.activity.BrowserActivity;
import com.limbo.showapp.base.BaseFragment;
import com.limbo.showapp.bean.BannerBean;
import com.limbo.showapp.bean.MenuBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.presenter.MenuPresenter;
import com.limbo.showapp.util.SPUtils;
import com.limbo.showapp.view.IHttpView;
import com.limbo.showapp.widget.OnMultiClickListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * Created by wangqi on 2018/9/4.
 */

public class MenuFragment extends BaseFragment<IHttpView, MenuPresenter> implements IHttpView<MenuBean> {

    private RecyclerView recycleView;

    @Override
    protected MenuPresenter getPresenter() {
        return new MenuPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_menu;
    }

    @Override
    public void initView(View view) {
        recycleView = view.findViewById(R.id.menu_recycle);
    }

    @Override
    public void initData() {
        //设置缓存数据
        MenuBean menuBean = (MenuBean) SPUtils.getObject(getContext(), AppConstant.SP.DATA_MAIN_MENU);
        setMenuData(menuBean);

        mPresenter.getMenu();
    }

    @Override
    public void onSuccess(MenuBean menuBean) {
        SPUtils.setObject(getContext(), AppConstant.SP.DATA_MAIN_MENU, menuBean);

        setMenuData(menuBean);
    }

    private void setMenuData(MenuBean menuBean) {
        if (menuBean == null)
            return;

        List<MenuBean.Menu> list = menuBean.getData();
        if (list != null && list.size() > 0) {
            recycleView.setLayoutManager(new GridLayoutManager(getContext(), list.size() <= 4 ? 4 : 5));
            recycleView.setAdapter(new CommonAdapter<MenuBean.Menu>(getContext(), R.layout.item_main_menu, list) {
                @Override
                protected void convert(ViewHolder holder, final MenuBean.Menu menu, int position) {
                    ImageView ivIcon = holder.getView(R.id.iv_icon);
                    TextView tvTitle = holder.getView(R.id.tv_title);

                    Glide.with(getContext()).load(AppConstant.Url.BASE_URL + menu.getImgUrl()).into(ivIcon);
                    tvTitle.setText(menu.getTitle());

                    holder.itemView.setOnClickListener(new OnMultiClickListener() {
                        @Override
                        public void onMultiClick(View v) {
                            if (menu.getClickType() == AppConstant.TYPE.TYPE_VIDEO) {
                                //视频
                                VideoActivity.OpenActivity(getContext());
                            } else if (menu.getClickType() == AppConstant.TYPE.TYPE_PICTURE) {
                                //图片
                                PictureActivity.OpenActivity(getContext());
                            } else if (menu.getClickType() == AppConstant.TYPE.TYPE_CARTOON) {
                                //漫画 pdf
                                CartoonActivity.OpenActivity(getContext());
                            } else if (menu.getClickType() == AppConstant.TYPE.TYPE_STORY) {
                                BrowserActivity.OpenActivity(getContext(), "https://tg.cngold.org/futures/m/jjqhtxz60.html");
                            }
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onError(String error) {

    }
}
